unit spr_ObjectHist;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, shbl_Directory, DBGridEhGrouping, PropFilerEh, XLSExportComp,
  PropStorageEh, FIBDatabase, pFIBDatabase, ExtCtrls, DB, FIBDataSet,
  pFIBDataSet, ActnList, Buttons, GridsEh, DBGridEh, StdCtrls, ComCtrls,
  ToolWin, Mask, DBCtrlsEh, DBLookupEh;

type
  TsprObjectHist = class(TshblDirectory)
    dsReferID: TFIBIntegerField;
    dsReferNAME: TFIBStringField;
    dsReferDESCRIPTION: TFIBStringField;
    lcbRelation: TDBLookupComboboxEh;
    Label1: TLabel;
    dsRelationList: TpFIBDataSet;
    dscRelationList: TDataSource;
    GroupBox4: TGroupBox;
    Panel1: TPanel;
    dbgAllowFields: TDBGridEh;
    Panel3: TPanel;
    ToolBar2: TToolBar;
    ToolButton13: TToolButton;
    ToolButton12: TToolButton;
    aAddField: TAction;
    aDelField: TAction;
    dbgLoggedFields: TDBGridEh;
    dsAllowField: TpFIBDataSet;
    dscAllowField: TDataSource;
    dsLoggedField: TpFIBDataSet;
    dscLoggedField: TDataSource;
    dsAllowFieldRDBFIELD_NAME: TFIBStringField;
    dsAllowFieldRDBDESCRIPTION: TFIBMemoField;
    dsAllowFieldRDBRELATION_NAME: TFIBStringField;
    dsLoggedFieldFIELD_NAME: TFIBStringField;
    dsLoggedFieldDESCRIPTION: TFIBStringField;
    dsLoggedFieldID: TFIBIntegerField;
    dsLoggedFieldRELATION_ID: TFIBIntegerField;
    dsRelationListRELATION_NAME: TFIBStringField;
    Splitter1: TSplitter;
    procedure aAddFieldExecute(Sender: TObject);
    procedure aDelFieldExecute(Sender: TObject);
    procedure dsAllowFieldBeforeOpen(DataSet: TDataSet);
    procedure dsLoggedFieldBeforeOpen(DataSet: TDataSet);
    procedure aInsertExecute(Sender: TObject);
    procedure aEditExecute(Sender: TObject);
    procedure dbgAllowFieldsDblClick(Sender: TObject);
    procedure dbgLoggedFieldsDblClick(Sender: TObject);
    procedure dsReferAfterPost(DataSet: TDataSet);
    procedure lcbRelationExit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  sprObjectHist: TsprObjectHist;

implementation

uses uMAIN, shbl_GridWindow;

{$R *.dfm}

procedure TsprObjectHist.aAddFieldExecute(Sender: TObject);
begin

  if dsAllowFieldRDBFIELD_NAME.IsNull then exit;

  if not dsLoggedField.Active then
    dsLoggedField.Open;

  dsLoggedField.Insert;
  dsLoggedFieldFIELD_NAME.AsString := dsAllowFieldRDBFIELD_NAME.AsString;
  dsLoggedFieldRELATION_ID.Value := dsReferID.Value;
  dsLoggedField.Post;
  dsAllowField.Next;
  dsAllowField.ReopenLocate('FIELD_NAME');
end;

procedure TsprObjectHist.aDelFieldExecute(Sender: TObject);
begin
  if dsLoggedFieldID.IsNull then exit;

  dsLoggedField.Delete;
  dsLoggedField.FullRefresh;
  dsAllowField.FullRefresh;

end;

procedure TsprObjectHist.dsAllowFieldBeforeOpen(DataSet: TDataSet);
begin
  dsAllowField.ParamByName('relation_name').Value := dsReferNAME.Value;
end;

procedure TsprObjectHist.dsLoggedFieldBeforeOpen(DataSet: TDataSet);
begin
  dsLoggedField.ParamByName('prelation_id').Value := dsReferID.Value;
end;

procedure TsprObjectHist.aInsertExecute(Sender: TObject);
begin
  dsAllowField.Close;
  dsLoggedField.Close;
  dsRelationList.ParamByName('prelation_name').Clear;
  dsRelationList.FullRefresh;
  lcbRelation.Enabled := true;
  inherited;
end;

procedure TsprObjectHist.aEditExecute(Sender: TObject);
begin
  dsAllowField.FullRefresh;
  dsLoggedField.FullRefresh;
  dsRelationList.ParamByName('prelation_name').Value := dsReferNAME.Value;
  dsRelationList.FullRefresh;
  lcbRelation.Enabled := false;
  inherited;
end;

procedure TsprObjectHist.dbgAllowFieldsDblClick(Sender: TObject);
begin
  aAddField.Execute;
end;

procedure TsprObjectHist.dbgLoggedFieldsDblClick(Sender: TObject);
begin
  aDelField.Execute;
end;

procedure TsprObjectHist.dsReferAfterPost(DataSet: TDataSet);
begin
  dsAllowField.FullRefresh;
  dsLoggedField.FullRefresh;
end;

procedure TsprObjectHist.lcbRelationExit(Sender: TObject);
begin
  if dsRefer.State = dsInsert then begin
    dsRefer.Post;
    dsRefer.Edit;
  end;
end;

end.
